package com.orange.wwwapi.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.user.UserCoupon;
import com.orange.core.pojo.req.usercoupon.UserCouponPageReq;
import com.orange.core.pojo.req.usercoupon.UserCouponRefundApplyReq;
import com.orange.core.service.user.UserCouponService;
import com.orange.core.util.PrincipalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户优惠券
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/userCoupon")
public class UserCouponController {

    private final UserCouponService userCouponService;

    /**
     * 列表
     */
    @PostMapping("/page")
    public R<Page<UserCoupon>> page(@RequestBody PageReqDto<UserCouponPageReq> req) {
        if (req.getCondition() == null) {
            req.setCondition(new UserCouponPageReq());
        }
        req.getCondition().setUserId(PrincipalUtil.getId());
        return R.ok(userCouponService.page(req));
    }

    /**
     * 详情
     */
    @GetMapping("/info/{id}")
    public R<UserCoupon> getUserCouponById(@PathVariable(value = "id") int id) {
        return R.ok(userCouponService.info(id));
    }

    /**
     * 申请退款
     */
    @PostMapping("/refund/apply")
    public R<Boolean> applyRefund(@RequestBody UserCouponRefundApplyReq req) {
        userCouponService.applyRefund(req);
        return R.ok(true);
    }
}
